package JUnit;


import java.io.*;
import java.net.*;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class testClient implements Runnable {
	static final String host = "localhost";
	static final int port = 9969;
	static final int numOftests = 37;
	static public String cookie =null;
	protected static String getCookie() {
		return cookie;
	}

	protected static void setCookie(String cookie) {
		if (cookie==null){
			testClient.cookie = cookie;
		}
	}

	static final List<String> HTTPlist = new ArrayList<String>();
	public static void main(String[] args) throws IOException {
		
		
		// Get host and port
	

		String endl = System.lineSeparator();
	
		// should succeed
		HTTPlist.add("POST /login.jsp HTTP/1.1" + endl + endl
				+ "UserName=yochailehman2&Phone=0546861622" + endl + "$");
		// should succeed
		HTTPlist.add("POST /login.jsp HTTP/1.1" + endl + endl
				+ "UserName=mosheCahlon&Phone=0546861699" + endl + "$");
		// wrong method
		HTTPlist.add("GET /login.jsp HTTP/1.1" + endl + endl
				+ "UserName=arieDerei2&Phone=0546861622" + endl + "$");
		// missing parameter
		HTTPlist.add("POST /login.jsp HTTP/1.1" + endl + endl
				+ "UserName=buzhyHertzog" + endl + "$");
		// unknown uri
		HTTPlist.add("POST /lggn.jsp HTTP/1.1" + endl + endl
				+ "UserName=arielSharon" + endl + "$");
		// should succeed
	//	HTTPlist.add("GET /login.jsp HTTP/1.1" + endl
	//			+ "Cookie: "+testClient.getCookie() + endl + "$");
		// Unauthorized
		HTTPlist.add("POST /create_group.jsp HTTP/1.1" + endl
				+ "Cookie: "+testClient.getCookie() + endl + endl
				+ "GroupName=group1&Users=tzipiLivni,barohMarzel" + endl + "$"
				+ endl);
		// should succeed
		HTTPlist.add("POST /login.jsp HTTP/1.1" + endl + endl
				+ "UserName=yochailehman2&Phone=0546861622" + endl + "$");
		// should success
		HTTPlist.add("POST /login.jsp HTTP/1.1" + endl + endl
				+ "UserName=tzipiLivni&Phone=0546861212" + endl + "$");
		// should succeed
		HTTPlist.add("POST /login.jsp HTTP/1.1" + endl + endl
				+ "UserName=barohMarzel&Phone=0546861231" + endl + "$");
		// should succeed
		HTTPlist.add("POST /create_group.jsp HTTP/1.1" + endl
				+ "Cookie: "+testClient.getCookie() + endl + endl
				+ "GroupName=group1&Users=tzipiLivni,barohMarzel" + endl + "$"
				+ endl);
		// should succeed
		HTTPlist.add("POST /create_group.jsp HTTP/1.1" + endl
				+ "Cookie: "+testClient.getCookie() + endl + endl
				+ "GroupName=group2&Users=tzipiLivni,yochailehman2" + endl
				+ "$" + endl);
		// missing parameters
		HTTPlist.add("POST /create_group.jsp HTTP/1.1" + endl
				+ "Cookie: "+testClient.getCookie() + endl + endl
				+ "Users=tzipiLivni,barohMarzel" + endl + "$" + endl);
		// unknnown user
		HTTPlist.add("POST /create_group.jsp HTTP/1.1" + endl
				+ "Cookie: "+testClient.getCookie() + endl + endl
				+ "GroupName=group6&Users=tzipiLivni,NaftalyBenet" + endl + "$"
				+ endl);
		// groupname taken
		HTTPlist.add("POST /create_group.jsp HTTP/1.1" + endl
				+ "Cookie: "+testClient.getCookie() + endl + endl
				+ "GroupName=group1&Users=tzipiLivni" + endl + "$" + endl);
		// should succeed
		HTTPlist.add("POST /list.jsp HTTP/1.1" + endl
				+ "Cookie: "+testClient.getCookie() + endl + endl + "List=Groups"
				+ endl + "$" + endl);
		// should succeed
		HTTPlist.add("POST /list.jsp HTTP/1.1" + endl
				+ "Cookie: "+testClient.getCookie() + endl + endl
				+ "List=Group&Group=group1" + endl + "$" + endl);
		// should succeed
		HTTPlist.add("POST /list.jsp HTTP/1.1" + endl
				+ "Cookie: "+testClient.getCookie() + endl + endl + "List=Users"
				+ endl + "$" + endl);
		// missing parameter
		HTTPlist.add("POST /list.jsp HTTP/1.1" + endl
				+ "Cookie: "+testClient.getCookie() + endl + endl + "List=Group"
				+ endl + "$" + endl);
		// should succeed
		HTTPlist.add("POST /send.jsp HTTP/1.1" + endl
				+ "Cookie:"+testClient.getCookie() + endl + endl
				+ "Type=Group&Target=group2&Content=Hello Group2" + endl + "$"
				+ endl);
		// should succeed
		HTTPlist.add("POST /send.jsp HTTP/1.1"
				+ endl
				+ "Cookie:"+testClient.getCookie()
				+ endl
				+ endl
				+ "Type=Direct&Target=0546861212&Content=Hello tzipi,lets make peace"
				+ endl + "$" + endl);
		// unknown target
		HTTPlist.add("POST /send.jsp HTTP/1.1" + endl
				+ "Cookie: "+testClient.getCookie() + endl + endl
				+ "Type=Direct&Target=0544434333&Content=bkljfdggddgdggddgdg"
				+ endl + "$" + endl);
		// unknown target
		HTTPlist.add("POST /send.jsp HTTP/1.1" + endl
				+ "Cookie: "+testClient.getCookie() + endl + endl
				+ "Type=Group&Target=group3&Content=bkljfdggddgdggddgdg" + endl
				+ "$" + endl);
		// unvalid type
		HTTPlist.add("POST /send.jsp HTTP/1.1"
				+ endl
				+ "Cookie: "+testClient.getCookie()
				+ endl
				+ endl
				+ "Type=lotr&Target=0546861212&Content=Hello tzipi,lets make peace"
				+ endl + "$" + endl);
		// missing parameter
		HTTPlist.add("POST /send.jsp HTTP/1.1" + endl
				+ "Cookie: "+testClient.getCookie() + endl + endl
				+ "Target=0546861212&Content=Hello tzipi,lets make peace"
				+ endl + "$" + endl);
		// should succeed
		HTTPlist.add("POST /add_user.jsp HTTP/1.1" + endl
				+ "Cookie: "+testClient.getCookie() + endl + endl
				+ "Target=group2&User=0546861231" + endl + "$" + endl);
		// Unauthorized
		HTTPlist.add("POST /add_user.jsp HTTP/1.1" + endl
				+ "Cookie: user_auth=5cf4d7e" + endl + endl
				+ "Target=group2&User=0546861699" + endl + "$" + endl);
		// missing parameter
		HTTPlist.add("POST /add_user.jsp HTTP/1.1" + endl
				+ "Cookie: "+testClient.getCookie() + endl + endl + "Target=group2"
				+ endl + "$" + endl);
		// already in the group
		HTTPlist.add("POST /add_user.jsp HTTP/1.1" + endl
				+ "Cookie: "+testClient.getCookie() + endl + endl
				+ "Target=group2&User=0546861231" + endl + "$" + endl);
		// target not exist
		HTTPlist.add("POST /add_user.jsp HTTP/1.1" + endl
				+ "Cookie: "+testClient.getCookie() + endl + endl
				+ "Target=group2&User=05469999" + endl + "$" + endl);
		// should succeed
		HTTPlist.add("POST /remove_user.jsp HTTP/1.1" + endl
				+ "Cookie: "+testClient.getCookie() + endl + endl
				+ "Target=group2&User=0546861231" + endl + "$" + endl);
		// Unauthorized
		HTTPlist.add("POST /remove_user.jsp HTTP/1.1" + endl
				+ "Cookie: user_auth=5cf4d7e" + endl + endl
				+ "Target=group2&User=0546861212" + endl + "$" + endl);
		// missing parameter
		HTTPlist.add("POST /remove_user.jsp HTTP/1.1" + endl
				+ "Cookie: "+testClient.getCookie() + endl + endl + "Target=group2"
				+ endl + "$" + endl);
		// target not exist
		HTTPlist.add("POST /remove_user.jsp HTTP/1.1" + endl
				+ "Cookie: "+testClient.getCookie() + endl + endl
				+ "Target=group2&User=05469999" + endl + "$" + endl);
		// should succeed(GROUP 2 MSG)
		HTTPlist.add("GET /queue.jsp HTTP/1.1" + endl
				+ "Cookie: "+testClient.getCookie() + endl + endl + "$" + endl);
		// should succeed(GROUP 2 MSG)
		HTTPlist.add("GET /queue.jsp HTTP/1.1" + endl
				+ "Cookie: "+testClient.getCookie() + endl + endl + "$" + endl);
		// no new msg
		HTTPlist.add("GET /queue.jsp HTTP/1.1" + endl
				+ "Cookie: "+testClient.getCookie() + endl + endl + "$" + endl);

		for (String msg : HTTPlist) {
			try {
				sendHttpToServer(host, port, msg);
				Thread.sleep(10);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		System.out
				.println("******************************************************************************************");
		System.out
				.println("*****************************start multy thread check*****************************");
		System.out
				.println("******************************************************************************************");

		for (int i = 0; i < 100; i++) {
			Runnable runable = new testClient();
			Thread newThread = new Thread(runable);
			newThread.run();
		}

		System.out.println("Exiting...");

	}

	private static void sendHttpToServer(String host, int port, String msg)
			throws UnsupportedEncodingException, IOException,
			InterruptedException {
		System.out
				.println("**************************************************************");
		System.out.println("Connecting to " + host + ":" + port);

		Socket clientSocket = null;
		PrintWriter out = null;
		// Trying to connect to a socket and initialize an output stream
		try {
			clientSocket = new Socket(host, port); // host and port
			out = new PrintWriter(clientSocket.getOutputStream(), true);
		} catch (UnknownHostException e) {
			System.out.println("Unknown host: " + host);
			System.exit(1);
		} catch (IOException e) {
			System.out
					.println("Couldn't get output to " + host + " connection");
			System.exit(1);
		}

		BufferedReader in = null;
		// Initialize an input stream
		try {
			in = new BufferedReader(new InputStreamReader(
					clientSocket.getInputStream(), "UTF-8"));
		} catch (IOException e) {
			System.out.println("Couldn't get input to " + host + " connection");
			System.exit(1);
		}

		System.out.println("sending to server:");

		System.out.println(msg);

		BufferedReader userIn = new BufferedReader(new InputStreamReader(
				System.in, "UTF-8"));
		msg = URLEncoder.encode(msg, "utf-8");
		System.out
				.println("----------------SERVER RESPONE:----------------------");

		out.println(msg);
		String line;
		while ((line = in.readLine()) != null) {
			try{
			String res = (URLDecoder.decode(line, "utf-8"));
			System.out.println(res);
			String ResultString = null;
				Pattern regex = Pattern.compile("SESSIONID=.*", Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE | Pattern.MULTILINE);
				Matcher regexMatcher = regex.matcher(res);
				if (regexMatcher.find()) {
					ResultString = regexMatcher.group();
				} 
			
				if (ResultString!= null){
					
					System.out.println(ResultString);
					testClient.setCookie(ResultString);
				}
			}
			catch(Exception e){
			}
		}
		userIn.close();
		// Close all I/O
		out.close();
		in.close();

		clientSocket.close();
	}

	@Override
	public void run() {
		try {

		int randomCommand = (int) Math.round(Math.random()
				* (HTTPlist.size() - 1));
		System.out.println(randomCommand);
			sendHttpToServer(host, port, HTTPlist.get(randomCommand));
		} catch (IOException| InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
}
